package com.szh.taobaounion.Presenter.impl;

import com.szh.taobaounion.Presenter.ITicketPresenter;
import com.szh.taobaounion.model.Api;
import com.szh.taobaounion.model.TicketParams;
import com.szh.taobaounion.model.domain.TicketResult;
import com.szh.taobaounion.utils.LogUtils;
import com.szh.taobaounion.utils.RetrofitManager;
import com.szh.taobaounion.utils.UrlUtils;
import com.szh.taobaounion.view.ITicketPagerCallBack;

import java.net.HttpURLConnection;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class TicketPresenterImpl implements ITicketPresenter {
    private ITicketPagerCallBack mViewCallBack=null;
    private String mCover=null;
    private TicketResult mTicketResult;

    enum LoadState{
        LOADING,SUCCESS,ERROR,NONE
    }
    private LoadState mCurrentState =LoadState.NONE;
    @Override
    public void getTicket(String title, String url, String cover) {
        this.onTicketLoading();
        this.mCover=cover;
        //LogUtils.d(TicketPresenterImpl.this,"url--->"+url);
        String ticketUrl = UrlUtils.getTicketUrl(url);

        Retrofit retrofit = RetrofitManager.getInstance().getmRetrofit();
        Api api = retrofit.create(Api.class);
        TicketParams params = new TicketParams(ticketUrl,title);
        Call<TicketResult> task = api.getTicket(params);
        task.enqueue(new Callback<TicketResult>() {
            @Override
            public void onResponse(Call<TicketResult> call, Response<TicketResult> response) {
                int code = response.code();
                LogUtils.d(TicketPresenterImpl.this,"Ticket code--->"+code);
                if (code == HttpURLConnection.HTTP_OK) {
                    //请求成功
                    mTicketResult = response.body();
                    //LogUtils.d(TicketPresenterImpl.this,"respond--->"+ mTicketResult);
                    //通知Ui更新数据
                    onTicketLoadedSucess();
                }
                else {
                    //请求失败
                    onLoadedTickedError();
            }
            }

            @Override
            public void onFailure(Call<TicketResult> call, Throwable t) {
                //失败
                onLoadedTickedError();
            }
        });
    }

    private void onTicketLoadedSucess() {
        if (mViewCallBack != null) {
            mViewCallBack.onTicketLoaded(mCover, mTicketResult);
        }else {
            mCurrentState=LoadState.SUCCESS;
        }
    }

    private void onLoadedTickedError() {
        if (mViewCallBack != null) {
            mViewCallBack.onError();
        }else {
            mCurrentState =LoadState.ERROR;
        }
    }

    @Override
    public void registerViewCallBack(ITicketPagerCallBack callBack) {
        this.mViewCallBack=callBack;
        if (mCurrentState != null) {
            //说明状态改变
            //更新Ui
            if (mCurrentState == LoadState.SUCCESS) {
              onTicketLoadedSucess();
            }
        } else if (mCurrentState == LoadState.ERROR) {
            onLoadedTickedError();
        } else if (mCurrentState == LoadState.LOADING) {
            onTicketLoading();
        }

    }

    private void onTicketLoading() {
        if (mViewCallBack != null) {
            mViewCallBack.onLoading();
        }else {
            mCurrentState =LoadState.LOADING;
        }
    }

    @Override
    public void unregisterViewCallBack(ITicketPagerCallBack callBack) {
        this.mViewCallBack=null;
    }
}
